<?php
use Monolog\Handler\StreamHandler;

require_once __DIR__.'/../src/bootstrap.php';

$app = Realisation\App::getInstance();

$app['monolog.api.logfile'] = sprintf(__DIR__.'/../app/logs/api_%s.log', $env);
$app['monolog.api.level'] = $app['debug']? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO;
$app['monolog.api'] = $app->share(function ($app) {
    $log = new $app['monolog.logger.class']('api');
    $handler = new StreamHandler($app['monolog.api.logfile'], $app['monolog.api.level']);
    $log->pushHandler($handler);

    return $log;
});

require_once __DIR__.'/../app/config/routes.php';

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app->after(function(Request $request, Response $response){
    if( $request->get('format') === 'json' and $request->query->has('callback') )
    {
        $callback = $request->query->get('callback');
        $content = sprintf('%s(%s)', $callback, $response->getContent());
        $response->headers->set('content-type', 'text/plain');
        $response->setContent($content);
    }
});

$app->run();
